import {
  Controller,
  HttpCode,
  HttpStatus,
  Post,
  UseGuards,
  Request,
} from '@nestjs/common';
import { AuthService } from '../service/auth.service';
import { LocalAuthGuard } from '../../guards/localGurd';
import { TokenService } from '../service/token.service';
import { Public } from '../../core/decorators/publicGuards';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private tokenService: TokenService,
  ) {}

  @Public()
  @UseGuards(LocalAuthGuard)
  @HttpCode(HttpStatus.OK)
  @Post('login')
  async login(@Request() req) {
    const { accessToken } = await this.tokenService.generateAccessToken(
      req.user,
    );
    return {
      access_token: accessToken.value,
      statusCode: HttpStatus.OK,
      message: '登录成功',
    };
  }
  @Post('logout')
  @HttpCode(HttpStatus.OK)
  async logout(@Request() req) {
    // 删除token
    return await this.tokenService.deleteUserToken(req.user.userId);
  }
}
